package com.zpp.wedding_admin.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zpp.wedding_admin.entity.User;
import com.zpp.wedding_admin.service.IUserService;
import com.zpp.wedding_admin.utils.MD5Utils;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "前台用户操作")
@CrossOrigin
@RestController
@RequestMapping("/weddingindex/user")
public class ApiUserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R login(@RequestBody User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        String password = MD5Utils.getCode(user.getPassword());
        lqw.eq(User::getUsername, user.getUsername());
        User one = userService.getOne(lqw);
        if ( one != null ) {
            boolean matches = MD5Utils.match(user.getPassword(), one.getPassword());
            if ( matches ) {
                return R.ok().data("user", one);
            } else {
                return R.ok().data("error_msg", "账号或密码错误");
            }
        } else {
            return R.ok().data("error_msg", "账号或密码错误");
        }
    }

    @ApiOperation(value = "注册")
    @PostMapping("register")
    public R register(@RequestBody User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper();
        lqw.eq(User::getUsername, user.getUsername());
        User one = userService.getOne(lqw);
        if ( one != null ) {
            return R.ok().data("error_msg", "该用户名已被注册");
        }
        user.setPassword(MD5Utils.getCode(user.getPassword()));
        boolean b = userService.save(user);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "更新信息")
    @PostMapping("update")
    public R update(@RequestBody User user) {
        user.setPassword(getPass(user.getPassword()));
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUserId, user.getUserId());
        boolean b = userService.update(user, lqw);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("updatePassword")
    public R updatePassword(@RequestBody User user) {
        String password = MD5Utils.getCode(user.getPassword());
        user.setPassword(password);
        boolean b = userService.update(user, new LambdaQueryWrapper<User>().eq(User::getUserId, user.getUserId()));
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "验证原密码正确性")
    @PostMapping("verifyPassword")
    public R verifyPassword(@RequestBody User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUserId, user.getUserId());
        User one = userService.getOne(lqw);
        boolean matches = MD5Utils.match(user.getPassword(), one.getPassword());
        return R.ok().data("matches", matches);
    }

    @ApiOperation(value = "用户参与活动加1")
    @GetMapping("JoinNumAdd/{id}")
    public R JoinNumAdd(@PathVariable Integer id) {
        boolean b = userService.JoinNumAdd(id);
        if ( b ) return R.ok();
        return R.error();
    }

    private String getPass(String password) {
        if (ObjectUtil.isEmpty(password)) return null;
        return MD5Utils.getCode(password);
    }
}
